package com.jcl.recruit.resume.logic;

import com.jcl.recruit.resume.model.*;
import com.jcl.recruit.server.elastic.SearchResumeServer;
import com.jcl.recruit.server.elastic.vo.*;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class ResumeTransferLogic {

    @Autowired
    private SearchResumeServer searchResumeServer;

    @Autowired
    private ResumeBasicInfoLogic resumeBasicInfoLogic;

    @Autowired
    private ResumePersInfoLogic resumePersInfoLogic;

    @Autowired
    private ResumeSelfEvaluationLogic resumeSelfEvaluationLogic;

    @Autowired
    private ResumeJobIntentionLogic resumeJobIntentionLogic;

    @Autowired
    private ResumeEduLogic resumeEduLogic;

    @Autowired
    private ResumeWorkLogic resumeWorkLogic;

    @Autowired
    private ResumeProjectLogic resumeProjectLogic;

    public ResumeBasicInfo transferResumeById(Integer searchResumeId, Integer category, String corpCode)
            throws Exception {

        ResumeBasicInfo resume = resumeBasicInfoLogic.findOneByExternalKey(corpCode, searchResumeId.toString());
        if (null != resume) {
            return resume;
        }

        SearchResumeResultEntity resumeResultEntity = searchResumeServer.findEntity(searchResumeId);
        if (null != resumeResultEntity) {
            resume = new ResumeBasicInfo();
            BeanUtils.copyProperties(resume, resumeResultEntity);
            resume.setCreateTime(new Date());
            resume.setUpdateTime(new Date());
            resume.setApplyOtherJid(searchResumeId.toString());
            resume.setCorpCode(corpCode);
            resumeBasicInfoLogic.add(resume);

            Integer resumeId = resume.getId();

            SearchResumePersInfo searchResumePersInfo = resumeResultEntity.getBasicInfo();
            SearchResumeJobIntention searchResumeJobIntention = resumeResultEntity.getJobIntention();
            SearchResumeSelfEvaluation searchResumeSelfEvaluation = resumeResultEntity.getSelfEvaluation();

            ResumePersInfo resumePersInfo = new ResumePersInfo();
            BeanUtils.copyProperties(resumePersInfo, searchResumePersInfo);
            resumePersInfo.setResumeId(resumeId);
            resumePersInfo.setCorpCode(corpCode);
            resumePersInfoLogic.add(resumePersInfo);

            ResumeJobIntention resumeJobIntention = new ResumeJobIntention();
            BeanUtils.copyProperties(resumeJobIntention, searchResumeJobIntention);
            resumeJobIntention.setResumeId(resumeId);
            resumeJobIntention.setCorpCode(corpCode);
            resumeJobIntentionLogic.add(resumeJobIntention);

            ResumeSelfEvaluation resumeSelfEvaluation = new ResumeSelfEvaluation();
            BeanUtils.copyProperties(resumeSelfEvaluation, searchResumeSelfEvaluation);
            resumeSelfEvaluation.setResumeId(resumeId);
            resumeSelfEvaluation.setCorpCode(corpCode);
            resumeSelfEvaluationLogic.add(resumeSelfEvaluation);

            List<SearchResumeEdu> eduList = resumeResultEntity.getEducations();
            List<SearchResumeWork> workList = resumeResultEntity.getWorks();
            List<SearchResumeProject> projectList = resumeResultEntity.getProjects();

            if (CollectionUtils.isNotEmpty(eduList)) {
                for (SearchResumeEdu edu : eduList) {
                    ResumeEdu _edu = new ResumeEdu();
                    BeanUtils.copyProperties(_edu, edu);
                    _edu.setResumeId(resumeId);
                    _edu.setCorpCode(corpCode);
                    resumeEduLogic.insert(_edu);
                }
            }
            if (CollectionUtils.isNotEmpty(workList)) {
                for (SearchResumeWork work : workList) {
                    ResumeWork _work = new ResumeWork();
                    BeanUtils.copyProperties(_work, work);
                    _work.setResumeId(resumeId);
                    _work.setCorpCode(corpCode);
                    resumeWorkLogic.insert(_work);
                }
            }
            if (CollectionUtils.isNotEmpty(projectList)) {
                for (SearchResumeProject project : projectList) {
                    ResumeProject _project = new ResumeProject();
                    BeanUtils.copyProperties(_project, project);
                    _project.setResumeId(resumeId);
                    _project.setCorpCode(corpCode);
                    resumeProjectLogic.isExitEntity(_project);
                }
            }

            return resume;
        }
        return null;
    }
}
